﻿//----------------------------------------------
//            NGUI: Next-Gen UI kit
// Copyright © 2011-2012 Tasharen Entertainment
//----------------------------------------------

using UnityEngine;
using UnityEditor;
using System;
using System.Collections.Generic;

/// <summary>
/// This script adds the NGUI menu options to the Unity Editor.
/// </summary>

static public class NGUIMenu
{
    /// <summary>
    /// Same as SelectedRoot(), but with a log message if nothing was found.
    /// </summary>

    static public GameObject SelectedRoot()
    {
        GameObject go = NGUIEditorTools.SelectedRoot();

        if (go == null)
        {
            //Debug.Log("No UI found. You can create a new one easily by using the UI creation wizard.\nOpening it for your onvenience.");
            Debug.Log("没有UI。您可以创建一个新的容易使用UI创建向导。\n为了您的方便，打开它。");
            CreateUIWizard();
        }
        return go;
    }

    //[MenuItem("NGUI/Attach a Collider #&c")]
    [MenuItem("NGUI/附加碰撞 #&c")]
    static public void AddCollider()
    {
        GameObject go = Selection.activeGameObject;

        if (NGUIEditorTools.WillLosePrefab(go))
        {
            if (go != null)
            {
                //NGUIEditorTools.RegisterUndo("Add Widget Collider", go);
                NGUIEditorTools.RegisterUndo("添加小部件碰撞", go);
                NGUITools.AddWidgetCollider(go);
            }
            else
            {
                //Debug.Log("You must select a game object first, such as your button.");
                Debug.Log("您必须先选择一个游戏对象，如你的按钮。");
            }
        }
    }

    static Vector3 Round(Vector3 v)
    {
        v.x = Mathf.Round(v.x);
        v.y = Mathf.Round(v.y);
        v.z = Mathf.Round(v.z);
        return v;
    }

    static void MakePixelPerfect(Transform t)
    {
        UIWidget w = t.GetComponent<UIWidget>();

        if (w != null)
        {
            w.MakePixelPerfect();
        }
        else
        {
            t.localPosition = Round(t.localPosition);
            t.localScale = Round(t.localScale);

            for (int i = 0, imax = t.childCount; i < imax; ++i)
            {
                MakePixelPerfect(t.GetChild(i));
            }
        }
    }

    //[MenuItem("NGUI/Make Pixel Perfect #&p")]
    [MenuItem("NGUI/对齐到像素 #&p")]
    static void PixelPerfectSelection()
    {
        if (Selection.activeTransform == null)
        {
            //Debug.Log("You must select an object in the scene hierarchy first");
            Debug.Log("您必须首先选择场景层次中的对象");
            return;
        }
        foreach (Transform t in Selection.transforms) MakePixelPerfect(t);
    }

    //[MenuItem("NGUI/Create a Panel")]
    [MenuItem("NGUI/创建面板")]
    static public void AddPanel()
    {
        GameObject go = SelectedRoot();

        if (NGUIEditorTools.WillLosePrefab(go))
        {
            //NGUIEditorTools.RegisterUndo("Add a child UI Panel", go);
            NGUIEditorTools.RegisterUndo("添加一个子界面面板", go);

            GameObject child = new GameObject(NGUITools.GetName<UIPanel>());
            child.layer = go.layer;

            Transform ct = child.transform;
            ct.parent = go.transform;
            ct.localPosition = Vector3.zero;
            ct.localRotation = Quaternion.identity;
            ct.localScale = Vector3.one;

            child.AddComponent<UIPanel>();
            Selection.activeGameObject = child;
        }
    }

    //[MenuItem("NGUI/Create a Widget")]
    [MenuItem("NGUI/创建小部件")]
    static public void CreateWidgetWizard()
    {
        //EditorWindow.GetWindow<UICreateWidgetWizard>(false, "Widget Tool", true);
        EditorWindow.GetWindow<UICreateWidgetWizard>(false, "部件工具", true);
    }

    //[MenuItem("NGUI/Create a New UI")]
    [MenuItem("NGUI/创建新UI")]
    static public void CreateUIWizard()
    {
        //EditorWindow.GetWindow<UICreateNewUIWizard>(false, "UI Tool", true);
        EditorWindow.GetWindow<UICreateNewUIWizard>(false, "界面工具", true);
    }

    //[MenuItem("NGUI/Panel Tool")]
    [MenuItem("NGUI/面板工具")]
    static public void OpenPanelWizard()
    {
        //EditorWindow.GetWindow<UIPanelTool>(false, "Panel Tool", true);
        EditorWindow.GetWindow<UIPanelTool>(false, "面板工具", true);
    }

    //[MenuItem("NGUI/Camera Tool")]
    [MenuItem("NGUI/相机工具")]
    static public void OpenCameraWizard()
    {
        //EditorWindow.GetWindow<UICameraTool>(false, "Camera Tool", true);
        EditorWindow.GetWindow<UICameraTool>(false, "相机工具", true);
    }

    //[MenuItem("NGUI/Font Maker #&f")]
    [MenuItem("NGUI/制作字体 #&f")]
    static public void OpenFontMaker()
    {
        //EditorWindow.GetWindow<UIFontMaker>(false, "Font Maker", true);
        EditorWindow.GetWindow<UIFontMaker>(false, "制作字体", true);
    }

    //[MenuItem("NGUI/Atlas Maker #&m")]
    [MenuItem("NGUI/制作图集 #&m")]
    static public void OpenAtlasMaker()
    {
        //EditorWindow.GetWindow<UIAtlasMaker>(false, "Atlas Maker", true);
        EditorWindow.GetWindow<UIAtlasMaker>(false, "制作图集", true);
    }
}